<template>
  <div class="article-detail-container">
    <div v-if="loading" class="loading">
      <el-skeleton :rows="10" animated />
    </div>
    <div v-else-if="error" class="error-message">
      {{ error }}
    </div>
    <div v-else class="article-content">
      <div class="article-header">
        <h1>{{ article.title }}</h1>
        <div class="article-meta">
          <span>作者: {{ article.author ? article.author.username : '未知' }}</span>
          <span>发布时间: {{ formatDate(article.createdAt) }}</span>
        </div>
      </div>
      
      <div class="article-body">
        {{ article.content }}
      </div>
      
      <div class="article-actions" v-if="isAuthor">
        <el-button type="primary" size="small" @click="editArticle">编辑</el-button>
        <el-button type="danger" size="small" @click="confirmDelete">删除</el-button>
      </div>
      
      <div class="comments-section">
        <h3>评论 ({{ comments.length }})</h3>
        <div v-if="comments.length === 0" class="no-comments">
          暂无评论
        </div>
        <div v-else class="comment-list">
          <div v-for="comment in comments" :key="comment.id" class="comment-item">
            <div class="comment-header">
              <span class="comment-author">{{ comment.author ? comment.author.username : '未知' }}</span>
              <span class="comment-time">{{ formatDate(comment.createdAt) }}</span>
            </div>
            <div class="comment-content">{{ comment.content }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ArticleDetail',
  data() {
    return {
      article: {},
      comments: [],
      loading: true,
      error: null,
      currentUser: null
    }
  },
  computed: {
    isAuthor() {
      return this.currentUser && this.article.author && this.currentUser.id === this.article.author.id;
    }
  },
  created() {
    // 获取当前登录用户信息
    const userInfo = localStorage.getItem('userInfo');
    if (userInfo) {
      this.currentUser = JSON.parse(userInfo);
    }
    
    this.fetchArticleData();
  },
  methods: {
    fetchArticleData() {
      const articleId = this.$route.params.id;
      this.loading = true;
      
      // 获取文章详情
      this.$axios.get(`/api/posts/${articleId}`)
        .then(response => {
          this.article = response.data;
          // 获取文章评论
          return this.$axios.get(`/api/comments?postId=${articleId}`);
        })
        .then(response => {
          this.comments = response.data;
          this.loading = false;
        })
        .catch(error => {
          console.error('获取文章详情失败:', error);
          this.error = '获取文章详情失败，请稍后重试';
          this.loading = false;
        });
    },
    formatDate(dateString) {
      if (!dateString) return '未知时间';
      const date = new Date(dateString);
      return date.toLocaleString('zh-CN');
    },
    editArticle() {
      this.$router.push(`/posts/edit/${this.article.id}`);
    },
    confirmDelete() {
      this.$confirm('确定要删除这篇文章吗？此操作不可逆', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteArticle();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    deleteArticle() {
      const articleId = this.article.id;
      this.$axios.delete(`/api/posts/${articleId}`)
        .then(() => {
          this.$message({
            type: 'success',
            message: '文章删除成功'
          });
          this.$router.push('/posts');
        })
        .catch(error => {
          console.error('删除文章失败:', error);
          this.$message({
            type: 'error',
            message: '删除文章失败，请稍后重试'
          });
        });
    }
  }
}
</script>

<style scoped>
.article-detail-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.article-header {
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
}

.article-meta {
  color: #909399;
  font-size: 14px;
  margin-top: 10px;
}

.article-meta span {
  margin-right: 15px;
}

.article-body {
  line-height: 1.8;
  font-size: 16px;
  margin-bottom: 30px;
}

.article-actions {
  margin-bottom: 30px;
}

.comments-section {
  margin-top: 40px;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.comment-item {
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px dashed #eee;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  font-size: 14px;
}

.comment-author {
  font-weight: bold;
  color: var(--primary-color);
}

.comment-time {
  color: #909399;
}

.no-comments {
  color: #909399;
  font-style: italic;
}

.loading, .error-message {
  padding: 20px;
  text-align: center;
}

.error-message {
  color: var(--danger-color);
}
</style>